using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace ModWatch
{

	/// <summary>
	/// Provides a base implementation for ModWatch controls that
	/// inherit <see cref="System.Web.UI.ExtenderControl"/>.
	/// </summary>
	public abstract class ModWatchExtenderBase : ExtenderControl
	{

		#region Events

		/// <summary>
		/// An event handler that allows child implementations to register
		/// <see cref="System.Web.UI.ScriptReference"/> objects with the
		/// <see cref="System.Web.UI.ExtenderControl"/>.
		/// </summary>
		public event EventHandler<ScriptReferencesEventArgs> GettingScriptReferences;

		#endregion

		#region ExtenderControl Implementation

		/// <summary>
		/// Provides an implementation of the
		/// <see cref="System.Web.UI.ExtenderControl.GetScriptReferences">GetScriptReferences</see>
		/// method of <see cref="System.Web.UI.ExtenderControl"/>.  Child implementations of
		/// <see cref="ModWatch.ModWatchExtenderBase"/> should use the
		/// <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences">GettingScriptReferences</see>
		/// event to register <see cref="System.Web.UI.ScriptReference"/> objects.
		/// The event based registration allows child implementations of
		/// <see cref="ModWatch.ModWatchExtenderBase"/> to add references to
		/// <see cref="System.Web.UI.ScriptReference"/> objects withing having to add 
		/// its parent's <see cref="System.Web.UI.ScriptReference"/> references as well.
		/// </summary>
		/// <returns></returns>
		protected sealed override IEnumerable<ScriptReference> GetScriptReferences()
		{
			ScriptReferencesEventArgs e = new ScriptReferencesEventArgs(this.Page);
			OnGettingScriptReferences(e);
			return e.GetScriptReferences();
		}

		#endregion

		#region Event Handlers

		/// <summary>
		/// Raises the <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/> event.
		/// </summary>
		/// <param name="e">
		///	A <see cref="ModWatch.ScriptReferencesEventArgs"/> object that contains the event data.
		/// </param>
		protected void OnGettingScriptReferences(ScriptReferencesEventArgs e)
		{
			if (GettingScriptReferences != null)
				GettingScriptReferences(this, e);
		}

		#endregion

		#region Internal Members

		/// <summary>
		/// Sets the ViewState value for the given property.
		/// </summary>
		/// <typeparam name="T">The type of the property.</typeparam>
		/// <param name="propertyName">The name of the property.</param>
		/// <param name="value">The value to store.</param>
		protected internal void SetProperty<T>(string propertyName, T value)
		{
			ViewState[propertyName] = value;
		}

		/// <summary>
		/// Returns the value of the given property.
		/// </summary>
		/// <typeparam name="T">The type of the propery.</typeparam>
		/// <param name="propertyName">The name of the property.</param>
		/// <param name="defaultValue">The default value of the property.</param>
		/// <returns></returns>
		protected internal T GetProperty<T>(string propertyName, T defaultValue)
		{
			if (ViewState[propertyName] == null)
				return defaultValue;
			else
				return (T)ViewState[propertyName];
		}

		#endregion

	}

}
